import axios from 'axios';
// import {Toast} from 'antd-mobile';
//     开发环境：http://wechat-dev.hongweimao.cn

// 生产环境：http://wechat.hongweimao.cn
let base_api = 'http://wechat.hongweimao.cn/api/';
// if (process.env.NODE_ENV !== 'development') {
//   base_api = 'http://wechat.hongweimao.cn' + base_api;
// }

// 创建axios实例
const http = axios.create({
  baseURL: base_api, // api的base_url
  timeout: 500000, // 请求超时时间
});

// console.log('process',process.env.NODE_ENV)

const errHandler = (err) => {

  if (err.name === 'SyntaxError') {
    return;
  }
  const response = err.response || {};
  const {
    code: errCode,
    message: errMsg
  } = response;
  let msg = '服务器未知异常';
  if (errMsg && String(errCode) !== '404') {
    msg = errMsg;
  }
  // Toast.fail(msg);
  // console.log(msg);
};

// request拦截器
http.interceptors.request.use((config) => {
  // const token = localStorage.getItem('LINZ_BLOG_TOKEN');
  // console.log(window.currentToken,'token');
  // if (window.currentToken) {
  //   // Bearer是JWT的认证头部信息
    config.headers.common['Authorization'] = 'a3daf0b873174a31a5da818a6363030c';
    config.headers.common['hwm-token'] = 'a3daf0b873174a31a5da818a6363030c';
  //   // Access-Control-Max-Age
 
  // }
  config.headers.common['Access-Control-Max-Age'] = 1728000;

  return config;
}, (error) => {
  return Promise.reject(error);
});

// respone拦截器
http.interceptors.response.use(
  response => {
    // 没登录 就移除本地的token
    // if (response.data && response.data.code === 403) {
    //   localStorage.removeItem('LINZ_BLOG_TOKEN');
    // }
    // return response.data;
    const res = response.data
    if (!res.success) {
      // Toast.fail(res.message);
      // console.log(window.location);
      
      // const href = window.location.protocol + '//wechat.hongweimao.cn/wc/index?pagego=' + btoa(window.location.hash.replace('#/',''));
      // // console.log(href);
      // setTimeout(() => {
      //   // console.log(href,'ttt');
      //   window.location.href = href;
      // }, 10);
      return {
        success: false
      };
    } 
    return res;
  },
  (error) => {
    console.log(`err ${error}`); // for debug
    errHandler(error);
    return Promise.reject(error);
  },
);

export default http;